package com.qf.ssm.service.impl;
/*
   @author 86155
   @ClassName: com.qf.ssm.service.impl.MarketingServiceImpl
   @Description: 该类的功能描述
   @date: 2021/10/13 9:22 
*/

import com.github.pagehelper.PageHelper;
import com.qf.ssm.common.exception.ServiceException;
import com.qf.ssm.common.request.ActivityRequestParams;
import com.qf.ssm.common.request.CustomerRequestParams;
import com.qf.ssm.common.vo.ResponseCode;
import com.qf.ssm.entity.Activity;
import com.qf.ssm.entity.Customer;
import com.qf.ssm.mapper.MarketingMapper;
import com.qf.ssm.service.MarketingService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MarketingServiceImpl implements MarketingService {
    @Resource
    private MarketingMapper marketingMapper;
    /**
     *满减活动表
     */
    @Override
    public List<Activity> listActivity(String startDate, String endDate, Integer status, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Activity> activities = marketingMapper.selectActivity(startDate, endDate, status);
        return activities;
    }

    @Override
    public Boolean addActivity(ActivityRequestParams activityRequestParams) {
        Integer insert;
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityRequestParams, activity);
        try {
            insert = marketingMapper.insertActivity(activity);
        } catch (ServiceException ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return insert > 0;
    }

    @Override
    public Boolean updateActivity(ActivityRequestParams activityRequestParams) {
        Integer update;
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityRequestParams, activity);
        try {
            update = marketingMapper.updateActivity(activity);
        } catch (ServiceException ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return update > 0;
    }

    @Override
    public Boolean deleteActivity(Long aid) {
        Integer delete;
        try {
            delete = marketingMapper.deleteActivity(aid);
        } catch (ServiceException ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return delete > 0;
    }

    /**
     *门店新客立减表
     */
    @Override
    public List<Customer> listCustomer(String startDate, String endDate, Integer status, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Customer> customers = marketingMapper.selectCustomer(startDate, endDate, status);
        return customers;
    }

    @Override
    public Boolean addCustomer(CustomerRequestParams customerRequestParams) {
        Integer insert;
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerRequestParams, customer);
        try {
            insert = marketingMapper.addCustomer(customer);
        } catch (ServiceException ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return insert > 0;
    }

    @Override
    public Boolean updateCustomer(CustomerRequestParams customerRequestParams) {
        Integer update;
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerRequestParams, customer);
        try {
            update = marketingMapper.updateCustomer(customer);
        } catch (ServiceException ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return update > 0;
    }

    @Override
    public Boolean deleteCustomer(Long cid) {
        Integer delete;
        try {
            delete = marketingMapper.deleteCustomer(cid);
        } catch (ServiceException ex) {
            throw new ServiceException(ResponseCode.ERROR);
        }
        return delete > 0;
    }

}
